package com.hunanwenli.controller.findPwd;

import com.hunanwenli.entity.UserInfo;
import com.hunanwenli.entity.UserPwd;
import com.hunanwenli.service.RedisService;
import com.hunanwenli.service.UserInfoService;
import com.hunanwenli.service.UserPwdService;
import com.hunanwenli.tool.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author wanzhenyu
 * @date 2021/5/31 21:03
 */
@Controller
@RequestMapping(value = "/findPwd")
public class FindPwdController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserPwdService userPwdService;
    @Autowired
    private RedisService redisService;


    @PutMapping(value = "/findPwd4/{phone}/{password}")
    @ResponseBody
    public Integer findPwd4(@PathVariable("phone") String phone,
                            @PathVariable("password") String password){
        UserInfo userInfo =null;
        String userInfoJson = redisService.get(phone);
        if (!Tools.isNullOrEmpty(userInfoJson)){
            userInfo = Tools.jsonToPojo(userInfoJson, UserInfo.class);
        }else {
            userInfo = userInfoService.selectByPhone(phone);
        }
        UserPwd userPwd = userPwdService.selectByPrimaryKey(userInfo.getId());
        userPwd.setPassword(Tools.getMD5(password));
        return userPwdService.updateByPrimaryKeySelective(userPwd);

    }


    @GetMapping(value = "/ajaxFindPwd/{phone}/{email}")
    @ResponseBody
    public String ajaxFindPwd(@PathVariable("phone") String phone,
                              @PathVariable("email") String email){
        if (email==null||phone==null){
            return "false";
        }
        UserInfo userInfo = null;
        String str = redisService.get(phone);
        if (!Tools.isNullOrEmpty(str)){
            System.out.println("redis调用");
            userInfo = Tools.jsonToPojo(str, UserInfo.class);
        }else {
            userInfo = userInfoService.selectByPhone(phone);
        }
       if (userInfo==null){
           return "false";
       }else if(userInfo.getEmail().equals(email)) {
           return "true";
       }else {
           return "false";
       }


    }

}
